package com.sendpo.zjx.sendpo.activity;

import android.Manifest;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.utils.Tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 朱俊鑫 on 2017/7/4.
 * information 引导页
 */

public class GuideActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_REQUEST_PERMISSION = 10101;
    @BindView(R.id.viewpager)
   public  ViewPager viewpager;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
         Tools.initStatus(this,R.color.black);
          initJuagementGuide();
          initComponent();
    }
     public void initJuagementGuide(){
         SharedPreferences sp= Tools.getSharedPreference(this, Tools.CONFIG);
         if(sp.getBoolean(Tools.FIRSTRUNNING,false)){
         Tools.redirectActivity(this,StartActivity.class);
             finish();
             return;
         }
         initRequestPermission();
     }

    public void initComponent(){
         List<View> views=new ArrayList<>();
        View image1=View.inflate(this, R.layout.guide_image1,null);
        View image2=View.inflate(this, R.layout.guide_image2,null);
        View image3=View.inflate(this, R.layout.guide_image3,null);
        Button rb=image3.findViewById(R.id.reidrect_bt);
        rb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
         Tools.redirectActivity(GuideActivity.this,MainActivity.class);
                   Map<String,Object> objectMap=new HashMap<>();
                  objectMap.put(Tools.FIRSTRUNNING,true);
                Tools.setSharedPreference(GuideActivity.this, Tools.CONFIG,objectMap);
                finish();
            }
        });
          views.add(image1);
        views.add(image2);
        views.add(image3);
      viewpager.setAdapter(new GuideViewpagerAdpater(views));

    }


    public class GuideViewpagerAdpater extends PagerAdapter{
        public List<View> imageViews;
           public GuideViewpagerAdpater(List<View> imageViews){
               this.imageViews=imageViews;
           }
        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
             View view=imageViews.get(position);
                container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //super.destroyItem(container, position, object);
            View view=imageViews.get(position);
             container.removeView(view);
        }
    }


    public void initRequestPermission(){
        ActivityCompat.requestPermissions(this,new String[]{
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.MODIFY_AUDIO_SETTINGS,
                Manifest.permission.WRITE_SETTINGS,
                Manifest.permission.READ_PHONE_STATE

        },REQUEST_CODE_REQUEST_PERMISSION);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                 switch (requestCode){
                     case REQUEST_CODE_REQUEST_PERMISSION:

                         break;
                 }
    }
}
